package dao.impl;

import dao.LessonDao;
import entity.Lesson;
import util.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;

public class LessonDaoImpl implements LessonDao {

/*
     查询所有可申请课程信息
*/
    @Override
    public ArrayList<Lesson> findLessons(String username) throws Exception {

        ArrayList<Lesson> list = new ArrayList<>();
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        ResultSet rs = null;
        try{
            //执行sql语句，并且接收返回的结果集
            String sql = "SELECT * FROM lesson where student = " + "'" + username + "'";
            rs = stat.executeQuery(sql);

            //处理结果集
            while(rs.next()) {
                String code = rs.getString("code");
                String name = rs.getString("name");
                Integer status = rs.getInt("status");
                String teacher = rs.getString("teacher");
                String student = rs.getString("student");
                String disapprovereason = null;
                if(rs.getString("disapprovereason")!=null){
                    disapprovereason = rs.getString("disapprovereason");
                }
                String applyreason = null;
                if(rs.getString("applyreason")!=null){
                    applyreason = rs.getString("applyreason");
                }
                //封装Lesson对象
                Lesson lesson= new Lesson(code,name,teacher,student,status,disapprovereason,applyreason);

                //将Lesson对象保存到集合中
                list.add(lesson);
            }

        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            JDBCUtils.release(rs,stat,conn);
        }
        //将集合对象返回
        return list;
    }

    @Override
    public Lesson findByCode(String code) {
        return null;
    }

    @Override
    public int disapprove(String lessoncode, String student, String disapprovereason) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        try {
//            执行SQL语句
            String str = "UPDATE lesson SET disapprovereason = " + "'" + disapprovereason + "'" + " WHERE code = "+ lessoncode + " and student = " + "'" + student + "'";
            System.out.println(str);
            int count = stat.executeUpdate(str);
            System.out.println(count == 1 ?"插入成功":"插入失败");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }

        return 0;
    }

    @Override
    public int apply(String lessoncode, String student, String applyreason) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        try {
//            执行SQL语句
            String str = "UPDATE lesson SET applyreason = " + "'" + applyreason + "'" + " WHERE code = "+ lessoncode + " and student = " + "'" + student + "'";
            System.out.println(str);
            int count = stat.executeUpdate(str);
            System.out.println(count == 1 ?"插入成功":"插入失败");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }

        return 0;
    }

    @Override
    public int insert(Lesson lesson) {
        return 0;
    }

    @Override
    public int update(Lesson lesson) {
        return 0;
    }

    @Override
    public int updatestatus(String lessoncode,String student,String status,boolean flag) throws Exception {

        int statusint = Integer.parseInt(status);
        if(flag==true){if (statusint==0){statusint=1;}else{statusint=0;}}

//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        try {
//            执行SQL语句
            String str = "UPDATE lesson SET status = " + statusint + " WHERE code = "+ lessoncode + " and student = " + "'" + student + "'";
            System.out.println(str);
            int count = stat.executeUpdate(str);
            System.out.println(count == 1 ?"插入成功":"插入失败");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }

        return 0;
    }
}